package com.td.filemanager.business.filethumbnail;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.provider.ComponentViewHolder;
import com.td.filemanager.provider.ListComponentProvider;
import com.td.library.config.FileType;
import com.td.library.data.FileData;
import com.td.library.utils.LogUtil;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;
import ohos.media.photokit.metadata.AVThumbnailUtils;

import java.io.File;
import java.util.List;

/**
 * 文件缩略图提供
 */
public class FileThumbnailDataProvider extends ListComponentProvider<FileData> {
    private static final String TAG = "FileThumbnailDataProvider";

    private static final int DEFAULT_THUMBNAIL_WIDTH = 300;

    private static final int DEFAULT_THUMBNAIL_HEIGHT = 300;

    /**
     * 实例化新的设备数据提供程序。
     *
     * @param context 上下文对象
     * @param listBean 文件数据
     * @param resourceId 资源id
     */
    public FileThumbnailDataProvider(Context context, List<FileData> listBean, int resourceId) {
        super(context, listBean, resourceId);
    }

    @Override
    protected void onItemDataBind(ComponentViewHolder componentViewHolder, FileData item, int position) {
        Text deviceText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_title);
        deviceText.setText(item.getName());
        Text infoText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_info);

        Text positionText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_position);
        positionText.setText("" + position);

        String deviceName = item.getDeviceData().isLocal() ? "本机" : item.getDeviceData().getDeviceName();
        infoText.setText("来源：" + deviceName);
        setThumbnailImg(componentViewHolder, item);
    }


    private void setThumbnailImg(ComponentViewHolder componentViewHolder, FileData item) {
        int typeId = -1;
        PixelMap resMap = null;
        switch (item.getFileType()) {
            case DOCUMENT:
                typeId = ResourceTable.Media_icon_file;
                break;
            case IMAGE:
                typeId = ResourceTable.Media_icon_img;
                break;
            case AUDIO:
                typeId = ResourceTable.Media_icon_music;
                break;
            case VIDEO:
                typeId = ResourceTable.Media_icon_video;
                break;
            case OTHER:
                typeId = ResourceTable.Media_icon_other;
                break;
            default:
                break;
        }
        Image image = (Image) componentViewHolder.getChildComponent(ResourceTable.Id_item_type);
        if (typeId != -1) {
            image.setPixelMap(typeId);
        } else {
            image.setPixelMap(null);
        }
        if (item.getFileType() == FileType.IMAGE) {
            image.setPixelMap(AVThumbnailUtils.createImageThumbnail(new File(item.getAbsolutePath()),
                new Size(DEFAULT_THUMBNAIL_WIDTH, DEFAULT_THUMBNAIL_HEIGHT)));
        } else if (item.getFileType() == FileType.VIDEO) {
            image.setPixelMap(AVThumbnailUtils.createVideoThumbnail(new File(item.getAbsolutePath()),
                new Size(DEFAULT_THUMBNAIL_WIDTH, DEFAULT_THUMBNAIL_HEIGHT)));
        }
    }

    private void showImg(Image image, String path) {
        LogUtil.error(TAG, " file " + path + " " + new File(path).exists() );
        ImageSource.SourceOptions opts = new ImageSource.SourceOptions();
        ImageSource imageSource = ImageSource.create(new File(path), opts);
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        decodingOptions.desiredSize = new Size(300, 300);
        PixelMap map = imageSource.createPixelmap(new ImageSource.DecodingOptions());
        image.setPixelMap(map);
    }
}
